import IPy


CONEXT = b'context %(context)s'
A = b'context %(ip)s'

# 以字典形式进行赋值
CONEXT = CONEXT %({b'context': b'aaaa'})
print(CONEXT)
print(A %({b'ip':b'192.0.0.0'}))

def get_hostnet_by_netmask(n):
    """根据掩码的int值返回对应的主机范围域

    :return:
    """
    n = 24
    m = 32 - n
    ret = 0
    while(m):
        m = m - 1
        ret = ret | 1 << m

    print(IPy.IPint(ret))


def fab(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n += 1


for n in fab(5):
    print(n)

from inspect import isgeneratorfunction

print(isgeneratorfunction(fab))

import types
print(isinstance(fab, types.GeneratorType))  #False,是生成器函数

print(isinstance(fab(5), types.GeneratorType))  #True，是生成器类型


def read_file(fpath):
    """通过yield实现每次读取一部分数据，减少一次获取超大内存数据的情况

    :param fpath:
    :return:
    """
    BLOCK_SIZE = 1024
    with open(fpath, 'rb') as f:
        while True:
            block = f.read(BLOCK_SIZE)
            if block:
                yield block
            else:
                return  #yield函数中如果执行return则直接抛出StopIteration终止迭代


